const db = require('../db');

const Project = {
    // 创建项目
    async create(data) {
        const { name, projectManager, builder, laborTeam, remark, status } = data;
        const [result] = await db.execute(
            `INSERT INTO project (name, projectManager, builder, laborTeam, remark, status)
       VALUES (?, ?, ?, ?, ?, ?)`,
            [name, projectManager, builder, laborTeam, remark, status || 1]
        );
        return result;
    },

    // 获取所有项目
    async getAll() {
        const [rows] = await db.execute(`SELECT * FROM project WHERE status != 2`);
        return rows;
    },

    // 根据 ID 获取项目
    async getById(id) {
        const [rows] = await db.execute(`SELECT * FROM project WHERE id = ?`, [id]);
        return rows[0];
    },

    // 更新项目
    async update(id, data) {
        const { name, projectManager, builder, laborTeam, remark, status, ended_at } = data;
        const [result] = await db.execute(
            `UPDATE project SET 
        name = ?, 
        projectManager = ?, 
        builder = ?, 
        laborTeam = ?, 
        remark = ?, 
        status = ?, 
        ended_at = ?
       WHERE id = ?`,
            [name, projectManager, builder, laborTeam, remark, status, ended_at, id]
        );
        return result;
    },

    // 删除项目（软删除）
    async remove(id) {
        const [result] = await db.execute(
            `UPDATE project SET status = 2 WHERE id = ?`,
            [id]
        );
        return result;
    },
};

module.exports = Project;
